/*给Vue实例添加原型属性，所有的Vue实例都拥有了$http属性*/
Vue.prototype.$http = axios;
axios.defaults.baseURL = "http://localhost:80";//换成你自己后台访问根URL
//这个参数，设置的是图片查看的全局服务器的地址
Vue.prototype.$fastBaseUrl = "http://47.95.117.210:8888";


/*axios前置拦截器， 只要你发送axios之前，都先执行此箭头函数*/
axios.interceptors.request.use(config => {
    //携带token
    let token = sessionStorage.getItem("token");
    if (token) {
        //添加请求头信息
        config.headers['token'] = token;
    }
    return config;
}, error => {
    Promise.reject(error);
});


//aixos后置拦截器，在响应目标函数之前，先执行此箭头函数
axios.interceptors.response.use(result => {
    let data = result.data;
    if (data && data.code === "1001") {
        //把token和登录用户删除掉
        sessionStorage.removeItem("token");
        //把登录用户删除吊
        sessionStorage.removeItem("loginInfo");
        //跳转到登录界面
        location.href = "/login.html";
        return;
    }
    return result;
}, error => {
    Promise.reject(error);
});

/**
 * js动态获取?后面的参数，并且封装成一个json对象
 * @returns {Object}
 */
function getParam(){
    var url=location.search;
    console.log(url)//?openid=oa9wA07lcp87qiB7xNxpJ2IM8pLY
    var param = new Object();
    if(url.indexOf("?") !== -1){ //url有?就会进这个if
        var str = url.substr(1)
        console.log(str) // openid=oa9wA07lcp87qiB7xNxpJ2IM8pLY
        strs = str.split("&"); //
        console.log(strs) //Array [ "openid=oa9wA07lcp87qiB7xNxpJ2IM8pLY" ]
        for(var i=0;i<strs.length;i++){
            var temp = strs[i]; //"openid=oa9wA07lcp87qiB7xNxpJ2IM8pLY"
            // temp.split("=")  {"openid","oa9wA07lcp87qiB7xNxpJ2IM8pLY"}
            var key = temp.split("=")[0]; //openid
            var value = temp.split("=")[1]; //oa9wA07lcp87qiB7xNxpJ2IM8pLY
            console.log(key,value); //openid oa9wA07lcp87qiB7xNxpJ2IM8pLY
            param[key]=unescape(value);//把这两个值变成的属性和值
        }
    }
    console.log(param)//Object { openid: "oa9wA07lcp87qiB7xNxpJ2IM8pLY" }
    return param; //{"openid":"oa9wA07lcp87qiB7xNxpJ2IM8pLY"}  {"id":2}

}